<template>
	<div class="addcomment w944">
		<div class="label_top">
			<button @click="reBack"><i><&nbsp;</i>返回</button>
			<p>{{courseName}}</p>
		</div>
		<div class="label_table">
			<alert ref="alert"></alert>
			<deleteAlert ref="deleteAlert"></deleteAlert>
			<el-table ref="multipleTable" :data="tableData" border @selection-change="handleSelectionChange">
				<el-table-column type="selection"></el-table-column>
				<el-table-column prop="nickname" label="用户名">
					<template slot-scope="scope">
						<span><img src="../../assets/qudaoImg/anonymous.png" v-if="scope.row.cryptonym==1">{{scope.row.nickname}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="content" label="评论"></el-table-column>
				<el-table-column label="操作">
					<template slot-scope="scope">
						<el-button @click="handleDelete(scope.row)" class="red">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
			<div class="fenyeMargin">
				<el-button @click="toggleSelection(tableData)" class="allSelect">全选</el-button>
				<el-button @click="handleDeleteAll()" class="allDelete">批量删除</el-button>
				<div class="pag_block">
					<el-pagination @current-change="handleCurrentChange" :current-page="currentpage" :page-size="pagesize" layout="prev, pager, next" :total="total"></el-pagination>
				</div>
			</div>
		</div>
	</div>
</template>
<script>
	import alert from '../../components/Alert.vue';
	import deleteAlert from '../../components/deleteAlert.vue';
	export default {
		data() {
			return {
				courseId:'', //课程编号id
				courseName:'', //课程名称
				teacherTitle:'',
				speakerId:'',
		        pagesize: 15,
		        currentpage: 1,
		        total: 1,
				id:'',
				tableData: [],
				tableChecked:[],
				ids:[],
				multipleSelection: [],
				deleteType:'delete'
			}
		},
		methods: {
			getParams() {
				this.courseId = this.$route.params.courseId;
				this.courseName = this.$route.params.courseName;
				this.teacherTitle = this.$route.params.teacherTitle;
				this.speakerId = this.$route.params.speakerId;
				console.log('addcomment:',this.$route.params);
			},
			getTableList(){
				this.$ajax({
					url: this.$root.jifuUrl + "/mns/kcCourseReview/kcCourseReviewList",
					headers:{'ContentType': 'application/json; charset=utf-8'},
					method: 'POST',
					data: {
						courseId:this.courseId,
						page:this.currentpage,
						num:this.pagesize,
						rootcompanyid:1
					},
					success: res => {
						if(res.data.code == 200) {
							this.tableData = res.data.data.list;
							this.total=res.data.data.pageParam.items;
						}
					}
				});
			},
			toggleSelection(rows) {
				if(rows) {
					rows.forEach(row => {
						this.$refs.multipleTable.toggleRowSelection(row, !this.allSelect)
					})
					this.allSelect = !this.allSelect;
				}
			},
			handleSelectionChange(val) {
				this.multipleSelection = [];
				for(var i = 0;i<val.length;i++){
					var s={"id":val[i].id}
					this.multipleSelection.push(s)
				}
			},
			handleDelete(row){
				this.transitionRow = row;
				this.$rootFunction().rootDelete();
			},
			handleDeleteAll() {
				this.$rootFunction().rootDeleteAll();
			},
			deleteSubmit() {
				if(this.deleteType == 'delete'){
					console.log('删除');
					let that = this,
						list = [];
					list.push({id:this.transitionRow.id});
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourseReview/deleteCourseReview",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:list,
						success: res => {
							if (res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}else{
					this.$ajax({ 
						url: this.$root.jifuUrl + "/mns/kcCourseReview/deleteCourseReview",
						headers:{'ContentType': 'application/json; charset=utf-8'},
						method: 'DELETE',
						data:this.multipleSelection,
						success: res => {
							if (res.data.code == 200) {
								this.$rootFunction().deleteResSuccess();
							}else{
								this.failText = res.data.message;
								this.$rootFunction().deleteResFail();
							}
						}
					});
				}
			},
			handleCurrentChange(value) {
				this.currentpage = value;
				this.getTableList();
			},
			stateWrapClose(){
				this.wrapClose = !this.wrapClose;
			},
			stateWrapCloseSecond() {
				this.wrapCloseSecond = !this.wrapCloseSecond;
			},
			reBack(){
				this.$router.go(-1);
			}
		},
		components: {
			alert,
			deleteAlert
		},
		mounted: function() {
			this.getParams();
			if(this.courseId==null||this.courseName==null){
				this.$store.commit('GET_comment');
				var s =this.$store.state.kcComment;
				this.courseId=s.courseId,
				this.courseName=s.courseName;
				this.teacherTitle = s.teacherTitle;
				this.speakerId = s.speakerId
			}
			this.getTableList();
			window.addEventListener("beforeunload",()=>{
				s={
					courseId : this.courseId,
					courseName:this.courseName,
					teacherTitle:this.teacherTitle,
					speakerId:this.speakerId
				}	
				this.$store.commit('SET_comment', s);
			});
		}
	}
</script>